use tracing::Level;
use tracing_subscriber::FmtSubscriber;
use crate::http::{init_gio_database, init_global_gio};

mod handler;
mod http;
mod template_engine;
mod util;
mod filter;
mod nosql;
mod http_client;

#[tokio::main]
async fn main() {

    // 设置日志记录器
    let subscriber = FmtSubscriber::builder()
        .with_max_level(Level::DEBUG)
        .finish();
    tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");

    // init_global_gio();

    init_gio_database();

    // // 启动 gio::run() 作为一个独立的任务
    // tokio::spawn(async move {
    //     nosql::gio::run().await
    // });

    // 启动 http::http_server::run() 作为一个独立的任务

    http::http_server::run().await;


}


#[cfg(test)]
mod tests {
    use std::collections::HashMap;
    use std::io::{Read, Write};
    use std::net::{TcpListener, TcpStream};
    use tracing::info;
    // 注意这个惯用法：在 tests 模块中，从外部作用域导入所有名字。
    use super::*;


    fn handle_client(mut stream: TcpStream) {
        let mut buffer = [0; 1024];
        match stream.read(&mut buffer) {
            Ok(_) => {
                let message = String::from_utf8_lossy(&buffer);
                info!("Received message: {}", message);
                if let Err(e) = stream.write_all(b"ojhuvfhckugvfcjycgjhk") {
                    eprintln!("Failed to send response: {}", e);
                }
                info!("Sent response");
            }
            Err(e) => {
                eprintln!("Failed to read from client: {}", e);
            }
        }
    }
    #[test]
    fn test_nosql() {

        let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
        println!("Server listening on port 8080");

        for stream in listener.incoming() {
            match stream {
                Ok(stream) => {
                    println!("New connection");
                    handle_client(stream);
                }
                Err(e) => {
                    eprintln!("Failed to accept connection: {}", e);
                }
            }
        }

    }

    #[test]
    fn test_client() {

        let mut stream = TcpStream::connect("127.0.0.1:8080").unwrap();
        println!("Connected to the server");

        if let Err(e) = stream.write_all(b"some_command") {
            eprintln!("Failed to send command: {}", e);
        }

        let mut buffer = [0; 1024];
        match stream.read(&mut buffer) {
            Ok(n) => {
                let response = String::from_utf8_lossy(&buffer[0..n]);
                println!("Received response: {}", response);
            }
            Err(e) => {
                eprintln!("Failed to read response: {}", e);
            }
        }

    }


    #[test]
    fn test_nosql_database() {

        let mut stream = TcpStream::connect("127.0.0.1:7423").unwrap();
        println!("Connected to the server");

        if let Err(e) = stream.write_all(b"some_command") {
            eprintln!("Failed to send command: {}", e);
        }

        let mut buffer = [0; 1024];
        match stream.read(&mut buffer) {
            Ok(n) => {
                let response = String::from_utf8_lossy(&buffer[0..n]);
                println!("Received response: {}", response);
            }
            Err(e) => {
                eprintln!("Failed to read response: {}", e);
            }
        }
    }
}